<template>
  <div>
    <router-link to="/index/menu/add">
      <el-button>添加</el-button>
    </router-link>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="ID" width="180">
      </el-table-column>
      <el-table-column prop="title" label="标题" width="180">
      </el-table-column>
      <el-table-column prop="type" label="类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1">目录</el-tag>
          <el-tag v-else type="success">菜单</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="图标">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="地址" ></el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
          <el-tag v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作"  align="center">
        <template slot-scope="scope">
          <el-button type="primary" circle size="mini"
            @click="edit(scope.row.id)" icon="el-icon-edit"></el-button>
          <el-button type="danger" circle  size="mini"
            @click="del(scope.row.id)" icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取表格的数据
    getData() {
      // this.$http.get('menulist',{params: {istree: 1}})
      this.$http.get('menulist?istree=1')
        .then(res => {
          this.tableData = res.list
        })
    },
    // 编辑
    edit(id) {
      this.$router.push('/index/menu/'+id);
    },
    // 删除,需要先弹窗二次确认
    del(id) {
      this.$confirm('确认关闭？')
        .then(_ => {
            // 确认删除
            this.doDel(id);
          })
        .catch(_ => {
          // 关闭弹窗
        });
    },
    doDel(id) {
      this.$http.post('/menudelete', {id})
        .then(res => {
          this.tableData = res.list;
        })
    }
  }
};
</script>
<style lang="">
</style>